package ro.ubbcluj.cs.damate.plugin.amq;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;
import ro.ubbcluj.cs.damate.plugin.events.PluginEvent;
import ro.ubbcluj.cs.damate.plugin.utils.DamateLogger;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.ObjectMessage;
import javax.jms.Session;

@Service
public class PluginEventProducer {
    
	@Autowired
    private DamateLogger log;

    @Autowired
    @Qualifier("myJmsTemplate")
    private JmsTemplate template;

    @Resource
    private Destination destination;

    @Autowired
    @Qualifier("pluginEventMessageSelector")
    private String messageSelectorId;

    public void sendEvent(final PluginEvent event) throws JMSException {
        template.send(destination, new MessageCreator() {
            public Message createMessage(Session session) throws JMSException {
                log.info("Putting event message into the queue: " + event);
                ObjectMessage message = session.createObjectMessage(event);
                message.setStringProperty("messageSelector", messageSelectorId);
                return message;
            }
        });
    }
}
